# -*- coding: utf-8 -*-
from gstreamer_lib.GStreamerWrapper import GStreamerWrapper
import cv2
import time

def main():
    print("Initializing GStreamerWrapper...")
    wrapper = GStreamerWrapper()

    # 从 GStreamerWrapper 实例中获取分辨率
    frame_width = wrapper.width
    frame_height = wrapper.height
    
    # 创建一个窗口用于显示视频流
    cv2.namedWindow("Live Video Feed", cv2.WINDOW_AUTOSIZE)

    print("\n------------------------------")
    print("Displaying live video. Press 'q' in the window to quit.")
    print("------------------------------")
    print(f"resolution: {frame_width}x{frame_height}")
    # 实时显示循环
    while True:
        frame = wrapper.GetFrame()

        if frame is not None:
            # 创建一个可写的副本以避免潜在的只读错误
            writable_frame = frame.copy()
            
            # 在窗口中显示帧
            cv2.imshow("Live Video Feed", writable_frame)
            
            # 检测按键，如果按下 'q' 键则退出循环
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        
    # 清理资源
    cv2.destroyAllWindows()
    print("\nVideo feed stopped.")

    # 清理 GStreamer 管道和线程
    print("Stopping GStreamer thread...")
    wrapper.StopThread()
    print("Test complete.")

if __name__ == "__main__":
    main()
